<?php
/**
 *快聚fun首页
 */
include 'inc/common.inc.php';
include 'MAccount.class.php';
include 'MCheckUser.class.php';
include 'SMS.class.php';

class Controller extends BaseController
{
	/**
	 *检查用户是否已经存在
	 */
	public function checktelAction()
	{
		//创建account的module类
		$ma = new MAccount();
		//获取前端参数
		$tel = $this->get_request('tel');
		
		if($ma->checkUser($tel))
		{
			$this->_msg->set(array('desc'=>'ok','return_code'=>1));
		}
		else
		{
			$this->_msg->set(array('desc'=>'user not exists', 'return_code'=>0));
		}
		echo $this->_msg;
	}
	
	/**
	 *检查用户的登入信息
	 */
	public function loginCheckAction()
	{
		$ma = new MAccount();
		$db = new MCheckUser();
		$sessionId = session_id();
		$tel = $this->get_request('userTel');
		$loginPwd = @$this->get_request('loginPwd');
		$registerPwd = @$this->get_request('registerPwd');
		$registerNewPwd = @$this->get_request('registerNewPwd');
		if(isset($loginPwd) && $loginPwd!=""){
			$loginPwd = md5($loginPwd);
			if($ma->authenticateUser($tel,$loginPwd)){
				$this->setUserSessionId(25200);
				$ma->setSessionId($tel,$sessionId);
				$this->_msg->set(array('desc'=>"ok","return_code"=>1));
			}else{
				$this->_msg->set(array('desc'=>"tel or password is wrong","return_code"=>0));
			}
		}else if(isset($registerPwd) && isset($registerNewPwd) && $registerPwd!="" && $registerNewPwd!=""){
			$registerNewPwd = md5($registerNewPwd);
			//测试暂时确定发送验证码为1234
			if($registerPwd=="1234"){
			//if($registerPwd==$_SESSION['verify']){
				$this->setUserSessionId(25200);
				$ma->insertUser($tel,$registerNewPwd);
				$ma->setSessionId($tel,$sessionId);
				$this->_msg->set(array('desc'=>"ok","return_code"=>1));
			}else{
				$this->_msg->set(array('desc'=>"验证码错误","return_code"=>0));
			}
		}else if(isset($registerPwd) && $registerPwd!=""){
			//测试暂时确定发送验证码为1234
			if($registerPwd=="1234"){
			//if($registerPwd==$_SESSION['verify']){
				$registerPwd = md5($registerPwd);
				$this->setUserSessionId(25200);
				$ma->insertUser($tel,$registerPwd);
				$ma->setSessionId($tel,$sessionId);
				$this->_msg->set(array('desc'=>"ok","return_code"=>1));
			}else{
				$this->_msg->set(array('desc'=>"doing","return_code"=>0));
			}
		}
		echo $this->_msg;
	}
	
	/**
	 *发送邮件给用户修改密码(未完成)
	 */
	public function sendEmailAction()
	{
		$smtp = new smtp($emailConfig['smtpserver'],$emailConfig['smtpserverport'],true,$emailConfig['smtpuser'],$emailConfig['smtppassword']);
		$smtp->debug = $emailConfig['debugmodel'];
		$title ='云记事本'.$username.'修改密码';
		$privateKey = code($email, 'ENCODE', $CONF['key'], 3600*24);
		$content = '<br><br><br><font color=red>亲爱的用户您好，以下为您的密码修改连接，请在24小时之内打开并修改您的密码(请勿回复)！</font><br/><a href="'.$CONF['webUrl'].'/chpwd.php?privateKey='.base64_encode($privateKey).'" target="_blank" >'.$CONF['webUrl'].'/chpwd.php?privateKey='.base64_encode($privateKey).'</a>' .
				'<br><br><div align="right" style="width:700px">云记事本管理员&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.date("Y-m-d H:i").'</div>';
		if($smtp->sendmail($email, $emailConfig['smtpusermail'], $title, $content, $emailConfig['mailtype'])) echo 1;
	}
	
	public function sendVerificationAction()
	{
		$userTel = $this->get_request('userTel');
		$verification = rand(1000,9999);
		$_SESSION['verify'] = $verification;
		$sms = new SMS();
		$sms->send_com_msg("感谢您号码为 $userTel 的手机注册【快聚FUN】。您的验证码是：$verification ，请完成验证", array($userTel));
		//需要针对发送失败进行重新发送
	}
	
	
}

$c = new Controller();
$c->run();
$dbCheck = new MCheckUser();
//若已经成功登陆，跳转到发起聚会页面
if($dbCheck->checkSessionId())
{
	header("Location: myinfo.php");
}

if(!$c->get_action_name())
{
	include template('index.html');
}
?>